var $express = require('express');
var $result = require('../../common/result');


var router = $express.Router();
var $random =  require('../../common/ramdom_util');
var redis = require('redis');
var $config = require('../../config');
var redisclient = redis.createClient($config.game.redis);

/* 创建房间 */
router.get('/create', function(req, res, next) {
    //FIXME 随机数生成房间号的问题，有冲突的可能，可以考虑换成房间号ID数组中替换
    roomId = $random.getRandomNum(10000,99999);
    redisclient.get(roomId,function (err,data) {
        if(data === null){
            redisclient.set(roomId,"roomInfo");
            res.send($result.success.replaceData({roomId:roomId}));
        }
    })

});

router.get('/join', function(req, res, next) {
    console.log("join roomId",req.query.roomId)
    roomId = req.query.roomId
    redisclient.get(roomId,function (err,data) {
        if(data===null){
            res.send($result.error.changeMessage("房间号不存在"));
        }else{
            res.send($result.success.replaceData({roomId:roomId}));
        }
    });

});


module.exports = router;
